﻿#pragma once
#ifndef SerialMap_hpp
#define SerialMap_hpp

#include<map>
#include<unordered_map>
#include"SerialBuffer.hpp"


namespace MMFSerialize{

    //序列化map
    template<typename KeyT, typename ValueT>
    SerialInBuffer& operator>>(SerialInBuffer& buff,  const std::map<KeyT,ValueT>& map)
    {
        //获取数量
        uint32_t count = (uint32_t)map.size();

        //序列化数量
        buff >> count;

        //序列化健值对
        for (const std::pair<KeyT,ValueT>& pair : map)
        {
            //序列化健
            buff >> pair.first;

            //序列化值
            buff >> pair.second;
        }

        return buff;
    }

    //反序列化map
    template<typename KeyT, typename ValueT>
    SerialOutBuffer& operator<<(SerialOutBuffer& buff,  std::map<KeyT,ValueT>& map)
    {
        //获取数量
        uint32_t count;

        //反序列化数量
        buff << count;

        //序列化健值对
        for (uint32_t i = 0; i < count; i++)
        {
            //序列化健
            KeyT key;
            buff << key;

            //序列化值
            ValueT val;
            buff << val;
            
            map.emplace(std::move(key), std::move(val));
        }

        return buff;
    }

    //序列化unordered_map
    template<typename KeyT, typename ValueT>
    SerialInBuffer& operator>>(SerialInBuffer& buff,  const std::unordered_map<KeyT,ValueT>& map)
    {
        //获取数量
        uint32_t count = (uint32_t)map.size();

        //序列化数量
        buff >> count;

        //序列化健值对
        for (const std::pair<KeyT,ValueT>& pair : map)
        {
            //序列化健
            buff >> pair.first;

            //序列化值
            buff >> pair.second;
        }

        return buff;
    }

    //反序列化unordered_map
    template<typename KeyT, typename ValueT>
    SerialOutBuffer& operator<<(SerialOutBuffer& buff,  std::unordered_map<KeyT,ValueT>& map)
    {
        //获取数量
        uint32_t count;

        //反序列化数量
        buff << count;

        //序列化健值对
        for (uint32_t i = 0; i < count; i++)
        {
            //序列化健
            KeyT key;
            buff << key;

            //序列化值
            ValueT val;
            buff << val;
            
            map.emplace(std::move(key), std::move(val));
        }

        return buff;
    }
}



#endif